package com.caigou.modules.query.ui.coldhot

import android.os.Bundle
import android.text.TextUtils
import androidx.activity.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.screenCapture
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.utils.MediaUtils
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.query.R
import com.caigou.modules.query.adpter.ColdHotItemAdapter
import com.caigou.modules.query.bean.ColdHotItemBean
import com.caigou.modules.query.databinding.ActivityColdHotBinding
import com.caigou.modules.query.viewmodel.ColdHotViewModel
import com.caigou.modules.query.widget.CountDialog
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2022/01/04
 */
@Route(path = RouterPath.Query.PAGE_COLD_HOT)
@AndroidEntryPoint
class ColdHotActivity : BaseActivity() {

    private lateinit var binding: ActivityColdHotBinding

    private val coldHotViewModel: ColdHotViewModel by viewModels()

    private var sort = 0
    private var count = 50


    private val mAdapter: ColdHotItemAdapter by lazy {
        ColdHotItemAdapter(mutableListOf())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityColdHotBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.coldHotLlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)

        binding.coldHotSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                fetchColdHotList()
            }
        }

        binding.coldHotRecyclerView.run {
            layoutManager = LinearLayoutManager(this@ColdHotActivity)
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(this@ColdHotActivity))
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val bean = mAdapter.getItem(position)
            ARouter.getInstance().build(RouterPath.Query.PAGE_COLD_HOT_DETAIL)
                .withInt(AppConstants.COLD_HOT_COUNT, count)
                .withString(AppConstants.COLD_HOT_NUMBER, bean.number)
                .navigation()
        }

        binding.coldHotRgTypeSelector.setOnCheckedChangeListener { group, checkedId ->
            sort = when (checkedId) {
                binding.coldHotBtnHot.id -> 0
                binding.coldHotBtnCold.id -> 1
                else -> 0
            }
            fetchColdHotList()
        }

        binding.coldHotBtnCount.setOnClickListener {
            with(CountDialog(this)) {
                btnConfirm.setOnClickListener {
                    count = etNum.text.toString().toInt()
                    binding.coldHotBtnCount.text = "${count}期"
                    fetchColdHotList()
                    dismiss()
                }
                show()
            }
        }

        binding.coldHotTvBack.setOnClickListener {
            finish()
        }

        binding.coldHotIvShare.setOnClickListener {
            val bitmap = binding.coldHotLlContainer.screenCapture()
            MediaUtils.saveBitmap(this, bitmap)
            showToast("截图已保存到系统相册")
        }

        fetchColdHotList()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(coldHotViewModel.coldHotListLiveData, ::handleGetColdHotList)
    }

    private fun handleGetColdHotList(result: Resource<MutableList<ColdHotItemBean>>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取冷热号码异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                result.data?.apply {
                    mAdapter.setList(this)
                }
            }
        }
    }

    private fun fetchColdHotList() {
        coldHotViewModel.getColdHotList(sort, count)
    }
}